package com.fruit.controller.wap.order;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.fruit.constants.AfterSalesServiceConstant;
import com.fruit.controller.base.BaseController;
import com.fruit.pojo.order.Order;
import com.fruit.pojo.refund.AfterSalesService;
import com.fruit.pojo.refund.Question;
import com.fruit.resolver.DefiniteException;
import com.fruit.service.order.OrderRefundManager;
import com.fruit.util.ResultInfo;

@Controller
@RequestMapping(value = "/wap/order/refund")
public class RefundController extends BaseController{

	@Autowired
	private OrderRefundManager orderRefundService;
	
	@RequestMapping
	public ModelAndView refund(Order o){
		ModelAndView view = this.getModelAndView();
			
		Map<String, Object> resMap = orderRefundService.getRefundApply(o);
		ResultInfo ri = (ResultInfo)resMap.get("result");
		if(ResultInfo.ERROR.equals(ri.getState()) ){
			throw new DefiniteException(ri.getMessage());
		}
		if("detail".equals(ri.getCode())){
			AfterSalesService as = (AfterSalesService)resMap.get("as");
			view.setViewName("redirect:/wap/order/refund/detail?code="+as.getCode());
			return view;
		}
		view.addObject("order", resMap.get("order"));
		view.addObject("ogList", resMap.get("ogList"));
		view.setViewName("refund/apply-refund");
		return view;
	}
	
	@RequestMapping(value="/apply",method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> apply(@RequestParam("imgs")MultipartFile[] imgs, AfterSalesService as){
		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap = orderRefundService.saveRefund(as, imgs);
		return resMap;
	}
	
	@RequestMapping(value="/detail",method = { RequestMethod.POST,RequestMethod.GET })
	public ModelAndView detail(AfterSalesService as){
		ModelAndView view = this.getModelAndView();
		
		Map<String, Object> resMap = orderRefundService.getRefundDetail(as);
		ResultInfo ri = (ResultInfo)resMap.get("result");
		if(ResultInfo.ERROR.equals(ri.getState()) ){
			throw new DefiniteException(ri.getMessage());
		}
		AfterSalesService asn = (AfterSalesService)resMap.get("asn");
		if(AfterSalesServiceConstant.TYPE_REFUND.equals(asn.getType())||AfterSalesServiceConstant.TYPE_RETURN_GOODS.equals(asn.getType())){
		view.addObject("asn", resMap.get("asn"));
		view.addObject("asr", resMap.get("asr"));
		view.addObject("user", resMap.get("user"));
		view.addObject("asgCnt", resMap.get("asgCnt"));
		view.addObject("asgList", resMap.get("asgList"));
		view.setViewName("refund/refund-detail");
		}
		return view;
	}
	
	@RequestMapping(value="/question",method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getQuestion(Question q){
		Map<String, Object> resMap = new HashMap<String, Object>();
		
		resMap = orderRefundService.getQuestion(q);
		
		return resMap;
	}
	
	@RequestMapping(value="/question/page",method = { RequestMethod.POST,RequestMethod.GET })
	public ModelAndView questionPage(AfterSalesService as){
		ModelAndView view = this.getModelAndView();
		Map<String, Object> resMap = new HashMap<String, Object>();
		
		resMap = orderRefundService.getQuestionPage(as);
		ResultInfo ri = (ResultInfo)resMap.get("result");
		if(ResultInfo.ERROR.equals(ri.getState()) ){
			throw new DefiniteException(ri.getMessage());
		}
		view.addObject("asn",resMap.get("asn"));
		view.setViewName("refund/additional");
		return view;
	}
	
	@RequestMapping(value="/question/add",method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> questionAdd(@RequestParam("imgs")MultipartFile[] imgs, AfterSalesService as){
		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap = orderRefundService.saveQuestion(as, imgs);
		return resMap;
	}
}
